package leetcode;

import java.util.Objects;

/**
 * 101 对称二叉树
 * 是否为镜像对称，判断左树的右节点要和右树的左节点一样，递归的终止条件为 判断是否到叶节点
 */
public class T101SymmetricBinaryTree {


    public static void main(String[] args) {
        TreeNode rootNode = new TreeNode(1);
        constructTreeFactory(rootNode, 2, 2);
        constructTreeFactory(rootNode.left, 3, 4);
        constructTreeFactory(rootNode.right, 4, 3);


        T101SymmetricBinaryTree symmetricBinaryTree = new T101SymmetricBinaryTree();
        boolean symmetric = symmetricBinaryTree.isSymmetric(rootNode);
        System.out.println(symmetric);

    }

    public static void constructTreeFactory(TreeNode rootNode,int left,int right){
        TreeNode seconedNodeLeft = new TreeNode(left);
        TreeNode seconedNodeRight = new TreeNode(right);
        rootNode.left = seconedNodeLeft;
        rootNode.right = seconedNodeRight;
    }


    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }
        return isSymmetric(root.left, root.right);

    }

    public boolean isSymmetric(TreeNode left,TreeNode right){
        if(Objects.isNull(left)&&Objects.isNull(right)){
            return true;
        }
        if(Objects.nonNull(left)&&Objects.nonNull(right)){
               if(left.val == right.val){
                   return isSymmetric(left.right, right.left) && isSymmetric(left.left, right.right);
               }
        }
        return false;
    }


}


